package GUI;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import Other.Glob;
import database.DatabaseExtension;
import database.MyClient;
import database.MyCountry;
import database.MyPayment;
import database.MyClient;
import database.MyTax;
import database.NewTirDataBase;
import enums.DialogType;
import enums.SearchType;

public class Client extends JFrame{
	
	//variable for singleton
			private static Client clientInstance = null;
			private JPanel contentPane;
			private JTextField tfCode;
			private JTextField tfName;
			//variable that represent this window frame
			private JFrame thisFrame = this;
			//variable to enable and disable the screen
			boolean disable;
			private JTextField tfAddress;
			private JTextField tfPostcode;
			private JTextField tfTown;
			private JTextField tfProvince;
			private JTextField tfEmail;
			private JTextField tfPaymentName;
			private JTextField tfNif;
			private JTextField tfFax;
			private JTextField tfPhone;
			private JTextField tfCountryCode;
			private JTextField tfCountryName;
			private JTextField tfPaymentCode;
			private JTextField tfTaxCode;
			private JTextField tfTaxName;
			private JTextField tfMaturity;
			private JTextField tfComment1;
			private JTextField tfComment2;
			
			private Client() {
				//set the title
				setTitle("Client");
				setVisible(true);
				//set resizable
				setResizable(false);
				setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
				setBounds(100, 100, 855, 518);
				contentPane = new JPanel();
				contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
				setContentPane(contentPane);
				JPanel leftPanel = new JPanel();
				leftPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
				JPanel buttonPanel = new JPanel();
				buttonPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
				
				JPanel rightPanel = new JPanel();
				rightPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
				GroupLayout gl_contentPane = new GroupLayout(contentPane);
				gl_contentPane.setHorizontalGroup(
					gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(buttonPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
									.addComponent(leftPanel, GroupLayout.PREFERRED_SIZE, 412, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(rightPanel, GroupLayout.PREFERRED_SIZE, 421, GroupLayout.PREFERRED_SIZE)))
							.addGap(28))
				);
				gl_contentPane.setVerticalGroup(
					gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(leftPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(rightPanel, GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(21))
				);
				
				JLabel lblEmail = new JLabel("Email");
				
				tfEmail = new JTextField();
				tfEmail.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfCode.requestFocus();				
						}
					}
				});
				tfEmail.setColumns(10);
				
				tfPaymentName = new JTextField();
				tfPaymentName.setEditable(false);
				tfPaymentName.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfEmail.requestFocus();				
						}
					}
				});
				tfPaymentName.setColumns(10);
				
				JLabel lblContact = new JLabel("Payment");
				
				JLabel lblNifcif = new JLabel("Nif,Cif");
				
				tfNif = new JTextField();
				tfNif.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfPaymentCode.requestFocus();				
						}
					}
				});
				tfNif.setColumns(10);
				
				tfFax = new JTextField();
				tfFax.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfNif.requestFocus();				
						}
					}
				});
				tfFax.setColumns(10);
				
				JLabel lblFax = new JLabel("Fax");
				
				JLabel label_4 = new JLabel("Phone");
				
				tfPhone = new JTextField();
				tfPhone.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfFax.requestFocus();				
						}
					}
				});
				tfPhone.setColumns(10);
				
				tfCountryCode = new JTextField();
				tfCountryCode.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							//enterDataSecondary(tfCountryCode, tfCountryName, SearchType.COUNTRY);
							ScreenHandling.enterData(tfCountryCode, tfCountryName, tfPhone, SearchType.COUNTRY);
							System.out.println("button is being pressed");
							
						}
					}
				});
				tfCountryCode.setColumns(10);
				
				JLabel lblCountry = new JLabel("Country");
				
				JButton sbCountry = new JButton("F5 Search");
				
				sbCountry.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						 Glob.type = SearchType.CLIENTCOUNTRY;
						 //when textcode focused and f5 pressed search dialog comes up
						search();
					}
				});
				
				tfCountryName = new JTextField();
				tfCountryName.setEditable(false);
				tfCountryName.setColumns(10);
				
				tfPaymentCode = new JTextField();
				tfPaymentCode.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							//enterDataSecondary(tfPaymentCode, tfPaymentName, SearchType.PAYMENT);
							ScreenHandling.enterData(tfPaymentCode, tfPaymentName, tfTaxCode, SearchType.PAYMENT);
							System.out.println("button is being pressed");
							
						}
					}
				});
				tfPaymentCode.getInputMap(tfPaymentCode.WHEN_FOCUSED)
				.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
				tfPaymentCode.getActionMap().put("", new AbstractAction() {
					 public void actionPerformed(ActionEvent e) {
						 
						 //when textcode focused and f5 pressed search dialog comes up
						 Glob.type= SearchType.CLIENTPAYMENT;
						 
						 search();
						
					    }
				});
				tfPaymentCode.setColumns(10);
				
				JButton sbPayment = new JButton("F5 Search");
				sbPayment.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						 Glob.type = SearchType.CLIENTPAYMENT;
						 //when textcode focused and f5 pressed search dialog comes up
						search();
					}
				});
				
				JLabel lblNewLabel = new JLabel("Tax");
				
				tfTaxCode = new JTextField();
				tfTaxCode.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							//enterDataSecondary(tfTaxCode, tfTaxName, SearchType.TAX);
							ScreenHandling.enterData(tfTaxCode, tfTaxName, tfEmail, SearchType.TAX);
							System.out.println("button is being pressed");
							
						}
					}
				});
				tfTaxCode.getInputMap(tfTaxCode.WHEN_FOCUSED)
				.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
				tfTaxCode.getActionMap().put("", new AbstractAction() {
					 public void actionPerformed(ActionEvent e) {
						 
						 //when textcode focused and f5 pressed search dialog comes up
						 Glob.type= SearchType.CLIENTTAX;
						 
						 search();
						
					    }
				});
				tfTaxCode.setColumns(10);
				
				JButton sbTax = new JButton("F5 Search");
				
				sbTax.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						 Glob.type = SearchType.CLIENTTAX;
						 //when textcode focused and f5 pressed search dialog comes up
						search();
					}
				});
				
				tfTaxName = new JTextField();
				tfTaxName.setEditable(false);
				tfTaxName.setColumns(10);
				
				GroupLayout gl_rightPanel = new GroupLayout(rightPanel);
				gl_rightPanel.setHorizontalGroup(
					gl_rightPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_rightPanel.createSequentialGroup()
							.addGap(41)
							.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblEmail, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
								.addGroup(Alignment.TRAILING, gl_rightPanel.createSequentialGroup()
									.addComponent(lblCountry, GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(tfCountryCode, GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(tfCountryName, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(sbCountry))
								.addGroup(gl_rightPanel.createSequentialGroup()
									.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING, false)
										.addComponent(lblFax, GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
										.addComponent(lblNewLabel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
										.addComponent(lblNifcif, GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
										.addComponent(lblContact, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addGroup(gl_rightPanel.createSequentialGroup()
											.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)))
									.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_rightPanel.createSequentialGroup()
											.addPreferredGap(ComponentPlacement.RELATED)
											.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
												.addGroup(gl_rightPanel.createSequentialGroup()
													.addComponent(tfNif, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
													.addPreferredGap(ComponentPlacement.RELATED, 144, Short.MAX_VALUE))
												.addGroup(gl_rightPanel.createSequentialGroup()
													.addGroup(gl_rightPanel.createParallelGroup(Alignment.TRAILING, false)
														.addGroup(gl_rightPanel.createSequentialGroup()
															.addComponent(tfPaymentCode, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
															.addPreferredGap(ComponentPlacement.RELATED)
															.addComponent(tfPaymentName, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
														.addGroup(gl_rightPanel.createSequentialGroup()
															.addComponent(tfTaxCode, 0, 0, Short.MAX_VALUE)
															.addPreferredGap(ComponentPlacement.RELATED)
															.addComponent(tfTaxName, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
															.addPreferredGap(ComponentPlacement.RELATED))
														.addGroup(gl_rightPanel.createSequentialGroup()
															.addComponent(tfEmail)
															.addPreferredGap(ComponentPlacement.RELATED)))
													.addGap(18)
													.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
														.addComponent(sbTax)
														.addComponent(sbPayment, GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)))))
										.addGroup(gl_rightPanel.createSequentialGroup()
											.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING, false)
												.addComponent(tfFax)
												.addComponent(tfPhone, GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
											.addPreferredGap(ComponentPlacement.RELATED, 148, Short.MAX_VALUE)))))
							.addGap(30))
				);
				gl_rightPanel.setVerticalGroup(
					gl_rightPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_rightPanel.createSequentialGroup()
							.addGap(39)
							.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_rightPanel.createSequentialGroup()
									.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_rightPanel.createSequentialGroup()
											.addGap(1)
											.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
												.addComponent(tfCountryCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
												.addComponent(tfCountryName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
										.addGroup(gl_rightPanel.createSequentialGroup()
											.addGap(4)
											.addComponent(lblCountry)))
									.addGap(23)
									.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
										.addComponent(label_4)
										.addComponent(tfPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addGap(23)
									.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblFax)
										.addComponent(tfFax, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addGap(18)
									.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_rightPanel.createSequentialGroup()
											.addGap(3)
											.addComponent(lblNifcif))
										.addComponent(tfNif, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
								.addComponent(sbCountry))
							.addGap(18)
							.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblContact)
								.addComponent(tfPaymentCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(tfPaymentName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(sbPayment))
							.addGap(18)
							.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(sbTax)
								.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
									.addComponent(lblNewLabel)
									.addComponent(tfTaxCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(tfTaxName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
							.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_rightPanel.createSequentialGroup()
									.addGap(14)
									.addComponent(lblEmail))
								.addGroup(gl_rightPanel.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(tfEmail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
							.addGap(23))
				);
				rightPanel.setLayout(gl_rightPanel);
				JButton btnSave = new JButton("F4 Save");
				btnSave.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						//Save when button pressed
						Glob.type = SearchType.CLIENT;
						try {
							saveDialog();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
				tfCode = new JTextField();
				
				tfCode.getInputMap(tfCode.WHEN_IN_FOCUSED_WINDOW)
				.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0), "Save");
				tfCode.getActionMap().put("Save", new AbstractAction() {
					 public void actionPerformed(ActionEvent e) {
						//Save when button pressed
						 Glob.type = SearchType.CLIENT;
						 try {
								saveDialog();
							} catch (Exception b) {
								// TODO Auto-generated catch block
								b.printStackTrace();
							}
					 }
				});
				JButton btnRemove = new JButton("F6 Remove");
				//set up F6 for removing
				btnRemove.getInputMap(btnRemove.WHEN_IN_FOCUSED_WINDOW)
				.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), "");
				btnRemove.getActionMap().put("", new AbstractAction() {
					 public void actionPerformed(ActionEvent e) {
						 //remove item when F6 button pressed
						ScreenHandling.remove(tfCode, SearchType.CLIENT);
					    }
				});
				btnRemove.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						//remove item when button pressed
						ScreenHandling.remove(tfCode, SearchType.CLIENT);
					}
				});
				JButton btnFReset = new JButton("F7 Reset");
				//set up F7 for reset
				btnFReset.getInputMap(btnFReset.WHEN_IN_FOCUSED_WINDOW)
				.put(KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0), "");
				btnFReset.getActionMap().put("", new AbstractAction() {
					 public void actionPerformed(ActionEvent e) {
						//reset field when button pressed
						 resetFields();
					    }
				});
				btnFReset.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						//reset field when button pressed
						resetFields();
					}
				});
				JButton btnEscExit = new JButton("Esc Exit");
				//set up Escape for exit
				btnEscExit.getInputMap(btnEscExit.WHEN_IN_FOCUSED_WINDOW)
				.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "");
				btnEscExit.getActionMap().put("", new AbstractAction() {
					 public void actionPerformed(ActionEvent e) {
						if (disable == false){
						//escape from the screen
							escape();
						}
					    }
				});
				btnEscExit.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						//if the screen is disable it is possible to escape
						if (disable == false){
							escape();
						}
					}
				});
				GroupLayout gl_buttonPanel = new GroupLayout(buttonPanel);
				gl_buttonPanel.setHorizontalGroup(
					gl_buttonPanel.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_buttonPanel.createSequentialGroup()
							.addGap(18)
							.addComponent(btnRemove)
							.addPreferredGap(ComponentPlacement.RELATED, 220, Short.MAX_VALUE)
							.addComponent(btnEscExit)
							.addGap(29)
							.addComponent(btnFReset)
							.addGap(34)
							.addComponent(btnSave)
							.addGap(18))
				);
				gl_buttonPanel.setVerticalGroup(
					gl_buttonPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_buttonPanel.createSequentialGroup()
							.addGap(21)
							.addGroup(gl_buttonPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnSave)
								.addComponent(btnRemove)
								.addComponent(btnFReset)
								.addComponent(btnEscExit))
							.addContainerGap(38, Short.MAX_VALUE))
				);
				buttonPanel.setLayout(gl_buttonPanel);
				JLabel lblCode = new JLabel("Code");
				JLabel lblName = new JLabel("Name");
				//set up F5 for saving only when the text is focused
				tfCode.getInputMap(tfCode.WHEN_FOCUSED)
				.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
				tfCode.getActionMap().put("", new AbstractAction() {
					 public void actionPerformed(ActionEvent e) {
						 Glob.type = SearchType.CLIENT;
						 //when textcode focused and f5 pressed search dialog comes up
						search();
					    }
				});
				JButton sbCode = new JButton("F5 Search");
				sbCode.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						 Glob.type = SearchType.CLIENT;
						 //when textcode focused and f5 pressed search dialog comes up
						search();
					}
				});
				tfCode.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a number is entered with ENTER
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							
							enterData();
							
							//tfName.requestFocus();
						}
						
					}
				});
				tfCode.setColumns(10);
				tfName = new JTextField();
				tfName.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a number is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfAddress.requestFocus();	
						}
					}
				});
				tfName.setColumns(10);
				
				JLabel lblAddress = new JLabel("Address");
				
				tfAddress = new JTextField();
				tfAddress.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfPostcode.requestFocus();				
						}
					}
				});
				
				tfAddress.setColumns(10);
				
				JLabel lblPostcode = new JLabel("Postcode");
				
				tfPostcode = new JTextField();
				tfPostcode.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfTown.requestFocus();				
						}
					}
				});
				tfPostcode.setColumns(10);
				
				JLabel lblTown = new JLabel("Town");
				
				tfTown = new JTextField();
				tfTown.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfProvince.requestFocus();				
						}
					}
				});
				tfTown.setColumns(10);
				
				JLabel lblProvince = new JLabel("Province");
				
				tfProvince = new JTextField();
				tfProvince.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfMaturity.requestFocus();				
						}
					}
				});
				//set up F5 for searching only when the text is focused
				tfCountryCode.getInputMap(tfCountryCode.WHEN_FOCUSED)
				.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
				tfCountryCode.getActionMap().put("", new AbstractAction() {
					 public void actionPerformed(ActionEvent e) {
						 
						 //when textcode focused and f5 pressed search dialog comes up
						 Glob.type= SearchType.CLIENTCOUNTRY;
						 
						search();
						
					    }
				});
				
				
				tfMaturity = new JTextField();
				tfMaturity.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfComment1.requestFocus();	
							System.out.println("Pressing maturity field");
						}
					}
				});
				tfProvince.setColumns(10);
				
				JLabel lblMaturity = new JLabel("Maturity");
				
			
				tfMaturity.setColumns(10);
				
				JLabel lblComment = new JLabel("Comment");
				
				tfComment1 = new JTextField();
				tfComment1.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfComment2.requestFocus();	
							System.out.println("Pressing comment1 field");
						}
					}
				});
				tfComment1.setColumns(10);
				
				tfComment2 = new JTextField();
				tfComment2.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a type is entered
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							//request focus for the next fields
							tfCountryCode.requestFocus();	
							System.out.println("Pressing comment2 field");
						}
					}
				});
				tfComment2.setColumns(10);
				GroupLayout gl_leftPanel = new GroupLayout(leftPanel);
				gl_leftPanel.setHorizontalGroup(
					gl_leftPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, gl_leftPanel.createSequentialGroup()
							.addGap(42)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblCode, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblName)
								.addComponent(lblAddress)
								.addComponent(lblPostcode)
								.addComponent(lblTown)
								.addComponent(lblProvince)
								.addComponent(lblMaturity)
								.addComponent(lblComment))
							.addPreferredGap(ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING, false)
								.addComponent(tfName)
								.addGroup(gl_leftPanel.createSequentialGroup()
									.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING)
										.addComponent(tfCode, GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
										.addComponent(tfPostcode)
										.addComponent(tfTown)
										.addComponent(tfProvince)
										.addComponent(tfMaturity, GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(sbCode, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
								.addComponent(tfAddress)
								.addComponent(tfComment1)
								.addComponent(tfComment2))
							.addGap(47))
				);
				gl_leftPanel.setVerticalGroup(
					gl_leftPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_leftPanel.createSequentialGroup()
							.addGap(37)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblCode)
								.addComponent(tfCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(sbCode))
							.addGap(18)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblName)
								.addComponent(tfName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblAddress)
								.addComponent(tfAddress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblPostcode)
								.addComponent(tfPostcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblTown)
								.addComponent(tfTown, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblProvince)
								.addComponent(tfProvince, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblMaturity)
								.addComponent(tfMaturity, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblComment)
								.addComponent(tfComment1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addComponent(tfComment2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(17))
				);
				//request focus when object is called
				tfCode.requestFocus(true);
				leftPanel.setLayout(gl_leftPanel);
				contentPane.setLayout(gl_contentPane);	
			}
		//singleton
		public static Client getInstance(){
			if(clientInstance == null){
				clientInstance = new Client();
			}
			return clientInstance;
			}
		//initiate data
		public void init(MyClient client){
			
		
			
			tfCode.setText(client.getClientID());	
			tfName.setText(client.getName());	
			tfAddress.setText(client.getAddress());
			tfPostcode.setText(client.getPostcode());
			tfTown.setText(client.getTown());
			tfProvince.setText(client.getProvince());
			tfMaturity.setText(client.getMaturity());
			tfComment1.setText(client.getComment1());
			tfComment2.setText(client.getComment2());
			if(String.valueOf(client.getCountryCodeID()).equals("null")){
				
				tfCountryCode.setText("");
				
			}else{
			tfCountryCode.setText(client.getCountryCodeID());
			}
			tfCountryName.setText(client.getCountryName());
			tfPhone.setText(client.getPhoneNumber());
			tfFax.setText(client.getFax());
			tfNif.setText(client.getNif());
			if(String.valueOf(client.getPaymentCodeID()).equals("null")){
				
				tfPaymentCode.setText("");
				
			}else{
				tfPaymentCode.setText(client.getPaymentCodeID());
			}
			tfPaymentName.setText(client.getPaymentName());
			if(String.valueOf(client.getTaxCodeID()).equals("null")){
				
				tfTaxCode.setText("");
				
			}else{
				tfTaxCode.setText(client.getTaxCodeID());
			}
			
			tfTaxName.setText(client.getTaxName());
			tfEmail.setText(client.getEmail());
			
		}
		
		public void initCountry(MyCountry country){
			
			tfCountryCode.setText(country.getCountryID());
			tfCountryName.setText(country.getCountryName());
			
		}
		
		public void initPayment(MyPayment payment){
			
			tfPaymentCode.setText(payment.getPaymentID());
			tfPaymentName.setText(payment.getPaymentName());
			
		}
		
		public void initTax(MyTax tax){
			
			tfTaxCode.setText(tax.getTaxID());
			tfTaxName.setText(tax.getTaxName());
			
		}
		
		
		//method needed to set focus from other screens
		public void setTextCodeFocus(){
			tfCode.requestFocus();
		}
		
		//reset fields
		public void resetFields(){
			tfCode.setText("");
			tfName.setText("");
			tfAddress.setText("");
			tfPostcode.setText("");
			tfTown.setText("");
			tfProvince.setText("");
			tfMaturity.setText("");
			tfComment1.setText("");
			tfComment2.setText("");
			tfCountryCode.setText("");
			tfCountryName.setText("");
			tfPhone.setText("");
			tfFax.setText("");
			tfNif.setText("");
			tfPaymentCode.setText("");
			tfPaymentName.setText("");
			tfTaxCode.setText("");
			tfTaxName.setText("");
			tfEmail.setText("");
			tfCode.requestFocus();
		}
		//method needed to get value from text code in other screens
		public String getFieldCode() {
		    return tfCode.getText();
		 }
		//method needed to get value from text Country field in other screens
		public String getFieldCountry() {
			return tfCountryCode.getText();
		}
		//method needed to get value from text Country field in other screens
			public String getFieldPayment() {
				return tfPaymentCode.getText();
		}
			
			//method needed to get value from text Country field in other screens
			public String getFieldTax() {
				return tfTaxCode.getText();
			}
		//method for saving the object
		public void saveDialog() throws Exception{
			//Glob.type = SearchType.TAX;
			Glob.setDialog(DialogType.OVERWRITE);
			try{
			if(tfName.getText().trim().length()<1) {
				JOptionPane.showMessageDialog(null, "Name may not be empty");
				tfName.requestFocus();
				return;
			}
			try{
			if(tfCode.getText().trim().length()>1) {
			if(Integer.parseInt(tfCode.getText())<=0){
				JOptionPane.showMessageDialog(null, "Code has to be a valid number");
				tfCode.setText("");
				tfCode.requestFocus();
				return;
				
			}
			}
			}catch(NumberFormatException n){
				
				JOptionPane.showMessageDialog(null, "Code number is to big");
				tfCode.setText("");
				tfCode.requestFocus();
				return;
				
			}
			
			String query = null;
			
			ScreenHandling.checkIdOnTable(tfCountryCode, SearchType.COUNTRY);
			
		
			ScreenHandling.checkIdOnTable(tfPaymentCode,  SearchType.PAYMENT );
			
		
			ScreenHandling.checkIdOnTable(tfTaxCode,  SearchType.TAX);
			
			
				
			ArrayList<Integer> ids = DatabaseExtension.columnIDs("Select * from client");
			//System.out.println(ids);
			boolean Ok = false;
			if(tfCode.getText().trim().length()<1) {
				//Do nothing
				}else{
					
			for (int i: ids) {
				if (i == Integer.parseInt(tfCode.getText()))
						Ok=true;
			}		
				if (Ok) {
				//JOptionPane.showMessageDialog(null, "This ID is not on the Tax table");
				Dialog d = Dialog.getInstance();
				d.setVisible(true);
				Client.getInstance().disableClient();
				//tfCode.requestFocus();
				return;
				
			}
				}
				if(Ok==false){
				save();			
				}
				
		}catch (NumberFormatException e){
			
			System.out.println("This is the general expetion inside saveDialog");
			JOptionPane.showMessageDialog(null, "Code must be a positive number");
			tfCode.setText("");
			tfCode.requestFocus();
			e.printStackTrace();
			return;
		 
		}
		}
		//method that saves the object in the database
		public void save(){
			
			Glob.setType(SearchType.CLIENT);
			
			String[] data = new String[16];	
			
			try{
			data[0] = tfCode.getText();	
			}catch(Exception code){
				
				code.printStackTrace();
				
			}
			System.out.println("This is the id got from the tfcode inside save" + data[0]);
			if(data[0].isEmpty()){
			data[0] = null;
			}
		data[1] = tfName.getText();
		
		data[2] = tfAddress.getText();
		
		data[3] = tfPostcode.getText();
		
		data[4] = tfTown.getText();
		
		data[5] = tfProvince.getText();
		
		data[6] = tfMaturity.getText();
		
		data[7] = tfComment1.getText();
		
		data[8] = tfComment2.getText();
		
		System.out.println("This is the length of the country code in the save method "+ tfCountryCode.getText().trim().length());
		
		try{
			if(tfCountryCode.getText().isEmpty()){
				
				data[9] = null;
				
			}else{
				//System.out.println("The ELSE has happeneded");
				data[9] = tfCountryCode.getText();
			}

		}catch(Exception country){
			country.printStackTrace();
			JOptionPane.showMessageDialog(null, "Country must be a proper integer number");
			return;
		}
		
		
		//data[7] = tfCountryName.getText();
		//we don't need data seven for saving, just for populating
		
		data[10] = tfPhone.getText();

		data[11] = tfFax.getText();
		
		data[12] = tfNif.getText();
		
		try{
			if(tfPaymentCode.getText().isEmpty()){
				
				data[13] = null;
				
			}else{
				//System.out.println("The ELSE has happeneded");
				data[13] = tfPaymentCode.getText();
			}

		}catch(Exception payment){
			payment.printStackTrace();
			JOptionPane.showMessageDialog(null, "Payment must be a proper integer number");
			return;
		}
		
		try{
			if(tfTaxCode.getText().isEmpty()){
				
				data[14] = null;
				
			}else{
				//System.out.println("The ELSE has happeneded");
				data[14] = tfTaxCode.getText();
			}

		}catch(Exception tax){
			tax.printStackTrace();
			JOptionPane.showMessageDialog(null, "Tax must be a proper integer number");
			return;
		}
		
		data[15] = tfEmail.getText();
		
		//System.out.println("This is the data String" + data[0]+data[1]+data[2]+data[3]+data[4]+data[5]+data[6]+data[7]+data[8]+data[9]+data[10]+data[11]+data[12]);
		
		//System.out.println("this is the string of data from Addressee in the method save" + data[0] + data[1] + data[2]);
			MyClient client = new MyClient(data);
			//System.out.println("this is the data inside de object in the method save" + Tax.getTaxID() + Tax.getTaxName());
			try{
				DatabaseExtension.saveClient(client);
			}catch (Exception saveClient){
				
				saveClient.printStackTrace();
				System.out.println("this is the exception released in saving an addressee");
				
				
			}
				resetFields();
				setTextCodeFocus();

		}
		//method that calls the searchwindow
		public void search(){
		
			SearchWindow s = SearchWindow.getInstance();
			s.setTitle();
			s.setVisible(true);
			disableClient();
		}
		//method to disable the screen
		public void disableClient(){
			Client.getInstance().disable();
			disable = true;
		}
		//method to enable the screen
		public void enableClient(){
			Client.getInstance().enable();
			disable = false;
			if (Glob.type ==  SearchType.CLIENT){
				tfCode.requestFocus();
			}else if (Glob.type ==  SearchType.CLIENTCOUNTRY){
				tfCountryCode.requestFocus();
			}else if (Glob.type ==  SearchType.CLIENTPAYMENT){
				tfPaymentCode.requestFocus();
			}else if (Glob.type ==  SearchType.CLIENTTAX){
				tfTaxCode.requestFocus();
			}
			
			Client.getInstance().toFront();
		}
		//method to get the disable boolean
		public boolean getDisable(){
			return disable;
		}

		//method that populates the screen fields with the object
		public void enterData(){
			try{
				//set up global variable for the Tax screen
				Glob.setType(SearchType.CLIENT);
				//if textfield is 0 or less than 0
				if(tfCode.getText().isEmpty()){
					tfName.requestFocus();
					
				}
				else if(Integer.parseInt(tfCode.getText()) <= 0){
					JOptionPane.showMessageDialog(null, "Code must be a positive number");
					tfCode.setText("");
					tfCode.requestFocus();
					return;
				}
			
			NewTirDataBase c = (MyClient) DatabaseExtension.getTableID("SELECT * FROM client WHERE clientID = '" + tfCode.getText() +"'")[0];
		
			//populating the fields
			tfName.setText(((MyClient) c).getName());
			tfAddress.setText(((MyClient) c).getAddress());
			tfPostcode.setText(((MyClient) c).getPostcode());
			tfTown.setText(((MyClient) c).getTown());
			tfProvince.setText(((MyClient) c).getProvince());
			tfMaturity.setText(((MyClient) c).getMaturity());
			try{
				//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
				if(String.valueOf(((MyClient) c).getCountryCodeID()).equals("null")){
					
					tfCountryCode.setText("");
					
				}else{
				tfCountryCode.setText(((MyClient) c).getCountryCodeID());
				}
			
				}catch(Exception e){
					System.out.println("This is the exception regarding the country code");
					e.printStackTrace();
				}
			
			tfComment1.setText(((MyClient) c).getComment1());
			tfComment2.setText(((MyClient) c).getComment2());
			tfCountryName.setText(((MyClient) c).getCountryName());
			tfPhone.setText(((MyClient) c).getPhoneNumber());
			tfFax.setText(((MyClient) c).getFax());
			tfNif.setText(((MyClient) c).getNif());
			
			try{
				//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
				if(String.valueOf(((MyClient) c).getPaymentCodeID()).equals("null")){
					
					tfPaymentCode.setText("");
					
				}else{
				tfPaymentCode.setText(((MyClient) c).getPaymentCodeID());
				}
			
				}catch(Exception e){
					System.out.println("This is the exception regarding the payment code");
					e.printStackTrace();
				}
			
			tfPaymentName.setText(((MyClient) c).getPaymentName());
			
			try{
				
				if(String.valueOf(((MyClient) c).getTaxCodeID()).equals("null")){
					
					tfTaxCode.setText("");
					
				}else{
				tfTaxCode.setText(((MyClient) c).getTaxCodeID());
				}
			
				}catch(Exception e){
					System.out.println("This is the exception regarding the tax code");
					e.printStackTrace();
				}
			
			
			
			tfTaxName.setText(((MyClient) c).getTaxName());
			
			
			tfEmail.setText(((MyClient) c).getEmail());
			
			
			//System.out.println("this is the Tax id and name" + c.getTaxID() + c.getTaxName());
			System.out.println("intro has been pressed");
			//requestfocus for the next field when pressing enter
			tfName.requestFocus();
			}catch(ArrayIndexOutOfBoundsException exception){
				System.out.println("This is the exception release when id not in database");
				//if there is no id in the database tfName set to empty
				//tfName.setText(null);
				exception.printStackTrace();
				//requestfocus for the next field when pressing enter
				tfName.requestFocus();
			}catch(NumberFormatException exception){
				tfCode.setText(null);
				System.out.println("This is the exception release when there is an String");
				exception.printStackTrace();
			}catch(Exception exception){
				System.out.println("General Exception");
				exception.printStackTrace();
			}
		}
		//method to close the screen
		public void escape(){
			Client.getInstance().dispose();
			Menu.getInstance().enableMenu();
		}
	
}
